package com.sohoometer.assignmeter.action;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.sohoometer.assignmeter.bean.AssignMeterDataExceptionResult;
import com.sohoometer.assignmeter.bean.ExceptionType;
import com.sohoometer.assignmeter.service.AssignMeterDataExceptionResultService;
import com.sohoometer.base.action.BaseAction;

@Controller
@Scope("prototype")
public class AssignMeterDataExceptionResultAction extends BaseAction {
	private static final long serialVersionUID = -6715672605989274025L;
	private String queryId = "13314";
	@Autowired
	private AssignMeterDataExceptionResultService resultService = null;
	private ResultWithPageCond<AssignMeterDataExceptionResult> resultWithPageCond;

	public AssignMeterDataExceptionResultAction() {
	}

	public String queryUnUpload() {
		setPage();
		resultWithPageCond = resultService.queryResultByQueryAndExceptionType(queryId, ExceptionType.NOT_UPLOAD, pageCond);
		this.pageSize = new BigDecimal(resultWithPageCond.getPageCond().getTotal()).divide(new BigDecimal(resultWithPageCond.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		return "unUpload";
	}

	public String queryTempException() {
		setPage();
		resultWithPageCond = resultService.queryResultByQueryAndExceptionType(queryId, ExceptionType.TEMP_EXCEPTION, pageCond);
		this.pageSize = new BigDecimal(resultWithPageCond.getPageCond().getTotal()).divide(new BigDecimal(resultWithPageCond.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		return "tempException";
	}

	public String queryReadNumException() {
		setPage();
		resultWithPageCond = resultService.queryResultByQueryAndExceptionType(queryId, ExceptionType.READNUM_EXCEPTION, pageCond);
		this.pageSize = new BigDecimal(resultWithPageCond.getPageCond().getTotal()).divide(new BigDecimal(resultWithPageCond.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		return "readNumException";
	}
	
	public String querySelfException() {
		setPage();
		resultWithPageCond = resultService.queryResultByQueryAndExceptionType(queryId, ExceptionType.SELF_EXCEPTION, pageCond);
		this.pageSize = new BigDecimal(resultWithPageCond.getPageCond().getTotal()).divide(new BigDecimal(resultWithPageCond.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		return "selfException";
	}

	private void setPage() {
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
	}
	
	public String getQueryId() {
		return queryId;
	}

	public void setQueryId(String queryId) {
		this.queryId = queryId;
	}

	public ResultWithPageCond<AssignMeterDataExceptionResult> getResultWithPageCond() {
		return resultWithPageCond;
	}

	public void setResultWithPageCond(
			ResultWithPageCond<AssignMeterDataExceptionResult> resultWithPageCond) {
		this.resultWithPageCond = resultWithPageCond;
	}

}
